package cn.yunhe.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;

import cn.yunhe.biz.IBedBiz;
import cn.yunhe.biz.ICarerBiz;
import cn.yunhe.biz.IDoctorBiz;
import cn.yunhe.biz.IHosregisterBiz;
import cn.yunhe.biz.IPresidentBiz;
import cn.yunhe.biz.IZhuYuanBiz;
import cn.yunhe.entity.Bed;
import cn.yunhe.entity.Carer;
import cn.yunhe.entity.Doctor;
import cn.yunhe.entity.Hosregister;
import cn.yunhe.entity.President;
import cn.yunhe.entity.Zhuyuan;

@Controller
public class ZhuYuanController {
	@Autowired//סԺ��
	@Qualifier("zyBiz")
	private IZhuYuanBiz zyBiz;
	public IZhuYuanBiz getZyBiz() {
		return zyBiz;
	}
	public void setZyBiz(IZhuYuanBiz zyBiz) {
		this.zyBiz = zyBiz;
	}
	
	@Autowired//�ҵ���
	@Qualifier("registerBiz")
	private IHosregisterBiz registerBiz;
	public IHosregisterBiz getRegisterBiz() {
		return registerBiz;
	}
	public void setRegisterBiz(IHosregisterBiz registerBiz) {
		this.registerBiz = registerBiz;
	}
	
	@Autowired//ҽ����
	@Qualifier("doctorBiz")
	private IDoctorBiz doctorBiz;
	public IDoctorBiz getDoctorBiz() {
		return doctorBiz;
	}
	public void setDoctorBiz(IDoctorBiz doctorBiz) {
		this.doctorBiz = doctorBiz;
	}
	
	@Autowired//���ұ�
	@Qualifier("presidentBiz")
	private IPresidentBiz presidentBiz;
	public IPresidentBiz getPresidentBiz() {
		return presidentBiz;
	}
	public void setPresidentBiz(IPresidentBiz presidentBiz) {
		this.presidentBiz = presidentBiz;
	}
	
	@Autowired//��λ��
	@Qualifier("bedBiz")
	private IBedBiz beBiz;
	public IBedBiz getBeBiz() {
		return beBiz;
	}
	public void setBeBiz(IBedBiz beBiz) {
		this.beBiz = beBiz;
	}

	@Autowired//����
	@Qualifier("carerBiz")
	private ICarerBiz carerBiz;
	public ICarerBiz getCarerBiz() {
		return carerBiz;
	}
	public void setCarerBiz(ICarerBiz carerBiz) {
		this.carerBiz = carerBiz;
	}
	
	
	
	@RequestMapping("/hosindex.do")
	public String hosindex(HttpSession session){
		List<Zhuyuan> list = zyBiz.FindAllZhuyuan();
		if(list!=null){
			for (Zhuyuan zhuyuan : list) {
				zhuyuan.setpName(presidentBiz.FindPresidentById(zhuyuan.getpId()).getpName());
				zhuyuan.setdName(doctorBiz.FindDoctorById(zhuyuan.getdId()).getdName());
			}
			session.setAttribute("zy", list);
			return "hospital/index";
		}
			return "index";
	}
	
	@RequestMapping("hosadd.do")
	public String hosadd(){
		return "hospital/add";
	}
	
	@ResponseBody
	@RequestMapping("/zhuyuanadd.do")
	public Object hosadds(Integer hosrId){
		Map map = new HashMap();
		Hosregister hosre = registerBiz.FindHosregister(hosrId);
		hosre.setHosrState(1);
		//�õ�ҽ������
		Doctor doctor = doctorBiz.FindDoctorById(hosre.getdId());
		hosre.setdName(doctor.getdName());
		
		//�õ���������
		President president = presidentBiz.FindPresidentById(hosre.getpId());
		hosre.setpName(president.getpName());
		
		if(hosre!=null){
			map.put("key",hosre);
		}else{
			map.put("key", null);
		}
		return JSONArray.toJSONString(map);
	}
	
	@ResponseBody
	@RequestMapping("/addbid.do")
	public Object addbid(){
		Map map = new HashMap();
	
		List<Bed> list = beBiz.FindAllBed();
		if(list!=null){
			map.put("key", list);
		}else{
			map.put("key", null);
		}
		return JSONArray.toJSONString(map);
	}
	
	@ResponseBody
	@RequestMapping("/addcid.do")
	public Object addcid(){
		Map map = new HashMap();
		List<Carer> list = carerBiz.FindAllCarer();
		if(list!=null){
			map.put("key", list);
		}else{
			map.put("key", null);
		}
		return JSONArray.toJSONString(map);
	}
	
	@RequestMapping("/sucaddzyuan.do")
	public String sucaddzy(Integer hosrId,Zhuyuan zhuyuan){
		Hosregister hosregister = registerBiz.FindHosregister(hosrId);
		zhuyuan.setHosrRegprice(hosregister.getHosrRegprice());
		zhuyuan.setHosrState(1);
		zhuyuan.setzTime(new Date());
		Integer flag = zyBiz.AddZhuYuan(zhuyuan);
		if(flag>0){
			return "redirect:/hosindex.do";
		}else{
			return "hospital/add";
		}
	}
	
	@RequestMapping("/lookzyedit.do")
	public String look(String action,Integer hosrId,HttpSession session){
		if("look".equals(action) || "edit".equals(action)){
			Zhuyuan zya = zyBiz.FindZhuyuanById(hosrId);
			if(zya!=null){
				zya.setpName(presidentBiz.FindPresidentById(zya.getpId()).getpName());
				zya.setdName(doctorBiz.FindDoctorById(zya.getdId()).getdName());
				session.setAttribute("zy", zya);
				if("look".equals(action)){
					return "hospital/look";
				}else{
					return "hospital/edit";
				}
			}
		}
		return "redirect:/hosindex.do";
		
	}
	
	@RequestMapping("/updazy.do")
	public String updazy(Zhuyuan zhuyuan,Integer hosrId){
		Zhuyuan zhuy = zyBiz.FindZhuyuanById(hosrId);	
		zhuyuan.setzTime(zhuy.getzTime());
		zhuyuan.setZprice(zhuy.getZprice());
		zhuyuan.setHosrState(zhuy.getHosrState());
		zhuyuan.setHosrRegprice(zhuy.getHosrRegprice());
		boolean flag= zyBiz.UpdateZhuyuanById(zhuyuan, hosrId);
		if(flag){
			return "redirect:/hosindex.do";
		}
			return "hospital/edit";
	}
	
	@RequestMapping("/dispensing.do")
	public String dispensing(){
		return "hospital/dispensing";
	}
	
	@RequestMapping("/updatazystate.do")
	public String updatazystate(Integer[] hosrId){
		Integer j = 0;
		for(Integer i=0;i<hosrId.length;i++){
			Zhuyuan zhuyuan = zyBiz.FindZhuyuanById(hosrId[i]);
			zhuyuan.setHosrState(2);
			Boolean flag = zyBiz.UpdateZhuyuanById(zhuyuan, hosrId[i]);
			if(flag){
				j++;
			}
		}
		if(j==hosrId.length){
			return "redirect:/hosindex.do";
		}else{
			return "redirect:/hosindex.do";
		}
	}
	
}
